package com.cx.mall.goods.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.cx.mall.common.entity.goods.entity.Category;
import com.cx.mall.common.entity.goods.model.CategoryDTO;
import com.cx.mall.goods.mapper.CategoryMapper;
import com.cx.mall.goods.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品类目 服务实现类
 * </p>
 *
 * @author zs
 * @since 2025-08-07
 */
@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public List<CategoryDTO> getCategoryList() {
        QueryChainWrapper<Category> chainWrapper = new QueryChainWrapper<>(baseMapper);
        //查询一级分类
        List<Category> categoryList = chainWrapper.isNull("parent_id").list();
        if(categoryList.size() == 0 || categoryList == null){
            log.info("category查询一级分类失败");
            return List.of(); //返回空集合
        }
        //转换成DTO
        List<CategoryDTO> oneCategoryDTOList = categoryList.stream().map((one ->
                        JSON.parseObject(JSON.toJSONString(one), CategoryDTO.class)
                )).collect(Collectors.toList());
        log.info("category查询一级分类成功,一级分类列表:{}",oneCategoryDTOList);
        //查询二级分类
        oneCategoryDTOList.forEach(one -> {
            QueryChainWrapper<Category> twoWrapper = new QueryChainWrapper<>(baseMapper);
            //获取二级分类列表
            List<Category> twoCategoryList = twoWrapper.eq("parent_id", one.getId()).list();
            if(twoCategoryList.size() == 0 || twoCategoryList == null){
                log.info("category查询二级分类失败");
                return; //返回空集合
            }
            //转换成DTO
            List<CategoryDTO> twoCategoryDTOList = twoCategoryList.stream().map((two ->
                            JSON.parseObject(JSON.toJSONString(two), CategoryDTO.class)
                    )).collect(Collectors.toList());
            log.info("category查询二级分类成功,二级分类列表:{}",twoCategoryDTOList);
            //查询三级分类
            twoCategoryDTOList.forEach(two -> {
                QueryChainWrapper<Category> threeWrapper = new QueryChainWrapper<>(baseMapper);
                //获取三级分类列表
                List<Category> threeCategoryList = threeWrapper.eq("parent_id", two.getId()).list();
                if(threeCategoryList.size() == 0 || threeCategoryList == null){
                    log.info("category查询三级分类失败");
                    return; //返回空集合
                }
                //转换成DTO
                List<CategoryDTO> threeCategoryDTOList = threeCategoryList.stream().map((three ->
                                JSON.parseObject(JSON.toJSONString(three), CategoryDTO.class)
                        )).collect(Collectors.toList());
                log.info("category查询三级分类成功,三级分类列表:{}",threeCategoryDTOList);
                //设置三级分类
                two.setCategoryList(threeCategoryDTOList);
            });
            //设置二级分类
            one.setCategoryList(twoCategoryDTOList);
        });
        return oneCategoryDTOList; //返回空集合
    }
}
